//
//  YZEPatientCollectionHeadView.m
//  yilutongxing
//
//  Created by 赵继申 on 17/3/21.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "YZEPatientCollectionHeadView.h"

@implementation YZEPatientCollectionHeadView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIImageView *bg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [bg setImage:[UIImage imageNamed:@"report_head_bg"]];
        [self addSubview:bg];
        
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        if (@available(iOS 11.0, *)) {
            _scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        [_scrollView setBackgroundColor:[UIColor clearColor]];
        _scrollView.userInteractionEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.directionalLockEnabled = YES;
        _scrollView.delegate = self;
        [self addSubview:_scrollView];
        
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, GetViewHeightFlaot(self)-40, GetViewWidthFlaot(self), 30)];  //创建UIPageControl，位置在屏幕最下方。
        _pageControl.pageIndicatorTintColor = [UIColor colorWithRed:147/255.0 green:169/255.0 blue:241/255.0 alpha:1.0];
        _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        [self addSubview:_pageControl];
        
    }
    return self;
}

-(void)setArray_Views:(NSMutableArray *)array_Views{
    if (array_Views.count == 0) {
        return;
    }
    _array_Views = array_Views;
    _scrollView.contentSize = CGSizeMake(self.frame.size.width*array_Views.count, 0);
    debugLog(@"%f--%f",self.frame.size.width,self.frame.size.width*array_Views.count);
    CGRect frame;
    frame.origin.y = 0;
    frame.size.height = GetViewHeightFlaot(self);
    frame.size.width = GetViewWidthFlaot(self);
    for (int i=0; i<array_Views.count; i++) {
        UIView *view = array_Views[i];
        frame.origin.x = GetViewWidthFlaot(self)*i;
        view.frame = frame;
        [_scrollView addSubview:view];
    }
    
    _pageControl.numberOfPages = array_Views.count;//总的页数
    _pageControl.currentPage = 0; //当前页
    
    
//    CGSize pointSize = [_pageControl sizeForNumberOfPages:array_Views.count];
//    
//    CGFloat page_x = -(_pageControl.bounds.size.width - pointSize.width) / 2 ;
//    
//    [_pageControl setBounds:CGRectMake(page_x, _pageControl.bounds.origin.y,
//                                      _pageControl.bounds.size.width, _pageControl.bounds.size.height)];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //更新UIPageControl的当前页
    CGPoint offset = scrollView.contentOffset;
    CGRect bounds = scrollView.frame;
    [_pageControl setCurrentPage:offset.x / bounds.size.width];
}
@end
